package com.google.gwt.livinglife.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.livinglife.client.LivingLife;
import com.google.gwt.livinglife.client.data.BasicPostData;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class BasicPostBox extends HorizontalPanel implements ClickHandler {
	private VerticalPanel main_body = new VerticalPanel();
	private Image author_picture = new Image();
	private Anchor author_anchor = new Anchor();
	private Label post_text = new Label();
	private Image picture = new Image();
	private FlowPanel author_text = new FlowPanel();

	public BasicPostBox() {
		main_body.add(author_text);
		author_text.add(author_anchor);
		author_text.add(post_text);
	}
	
	public void setBlank() {
		clear();
	}
	
	public void draw(BasicPostData post) {
		clear();
		if (post == null) {
			return;
		}
		setAuthorPicture(post);
		author_anchor.setText(post.getAuthor().getNickname());
		author_anchor.setHref(LivingLife.getUserIdUrl(post.getAuthor().getId()));
		post_text.setText(post.getPost_text());
		if (post.getEmbeded_image() != null && post.getEmbeded_image().length() != 0) {
			picture.setUrl(post.getEmbeded_image());
			main_body.add(picture);
		} else {
			main_body.remove(picture);
		}
		add(author_picture);
		add(main_body);
	}

	private void setAuthorPicture(BasicPostData post) {
		if (post.getAuthor().getPicture_url() == null || post.getAuthor().getPicture_url().isEmpty()) {
			author_picture.setUrl("images/face.png");
		} else {
			author_picture.setUrl(post.getAuthor().getPicture_url());
		}
		author_picture.setWidth("100px");
	}
	public void draw_fixed_size(BasicPostData post, int height_in_pixel, int width_in_pixel, int char_limit) {
		clear();
		if (post == null) {
			return;
		}
		setAuthorPicture(post);
		String text = post.getPost_text();
		if (text.length() + post.getAuthor().getNickname().length() > char_limit) {
			int text_length = char_limit - post.getAuthor().getNickname().length() - 3;
			if (text_length > 0) {
				text_length = 0;
			}
			text = text.substring(0, text_length) +  "...";
		}
		author_anchor.setText(post.getAuthor().getNickname());
		author_anchor.setHref(LivingLife.getUserIdUrl(post.getAuthor().getId()));
		post_text.setText(text);
		int flow_panel_ht = author_text.getOffsetHeight();
		// We would display any image if we have atleat 100 oixels of height for it.
		if (height_in_pixel > flow_panel_ht + 100 &&
				post.getEmbeded_image() != null &&
				post.getEmbeded_image().length() != 0) {
			picture.setUrl(post.getEmbeded_image());
			main_body.add(picture);
		} else {
			main_body.remove(picture);
		}
		add(author_picture);
		add(main_body);
	}

	@Override
	public void onClick(ClickEvent event) {
		
	}
}

